home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char *SCCSid = "%W% (NCSA) %G%";
- #endif
- /*
- * binsubs.c
- * by Gaige B. Paulsen
- ****************************************************************************
- * *
- * Uses : *
- * TCP/IP kernel for NCSA Telnet *
- * by Tim Krauskopf *
- * with Macintosh code by Gaige B. Paulsen *
- * *
- * National Center for Supercomputing Applications *
- * 152 Computing Applications Building *
- * 605 E. Springfield Ave. *
- * Champaign, IL 61820 *
- * *
- * *
- ****************************************************************************
- *
- * MacBinary Subroutines.
- *
- * Called by:
- * bkgr.c
- */
-
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Files.h>
- #include <Packages.h>
- #include <Memory.h>
-
- #include <stdio.h>
- #include <fcntl.h>
- #include "mpw.h"
- #include <string.h>
- #include <strings.h>
- #include "configrec.h"
- #include "maclook.h"
- #include "bkgr.h"
- #include "MacBinary.h"
-
- extern MBFile /* BYU */
- *ftp_mbfp, /* BYU */
- *mbfp; /* BYU */
-
- char *strncpy();
-
- #define BLOCKS(x) ((x+127)/128)
- #define lmove(f,t) movmem(f,t,4)
-
- #define MyFavouriteTextEditor '????'
-
- MBHead
- *mbh;
- char
- buffer[128];
-
- char *strsave( p)
- char *p;
- {
- char *t;
-
- t=NewPtr(strlen(p));
- return strncpy(t,p,strlen(p));
- }
-
- GetFileInfo(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- {
- iop->ioNamePtr = name;
- iop->ioVRefNum=vol;
- iop->ioFVersNum=iop->ioFDirIndex=0;
- PBGetFInfo((ParmBlkPtr) iop, FALSE);
- }
-
- SetFileInfo(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- {
- iop->ioNamePtr = name;
- iop->ioVRefNum=vol;
- iop->ioFVersNum=iop->ioFDirIndex=0;
- PBSetFInfo((ParmBlkPtr) iop, FALSE);
- }
-
- MakeTextFile(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- { GetFileInfo(vol,name,iop);
- iop->ioFlFndrInfo.fdType = 'TEXT';
- iop->ioFlFndrInfo.fdCreator = MyFavouriteTextEditor;
- SetFileInfo(vol,name,iop);
- }
-
- isMacBinary(p)
- MBHead *p;
- {
- return( (p->nlen > 0) &&
- (p->nlen < 65) &&
- (p->zero1 == 0) &&
- (p->zero2 == 0) &&
- (p->zero3 == 0));
- }
-
- int MBsize
- (
- MBFile *mbfp
- )
- {
- long int size;
- int ret;
-
- size = 0;
-
- ret = GetEOF( mbfp->fd, &size ); /* length of file data fork */
- if (ret != noErr)
- size = 0;
-
- return(size);
- }
-
- MBFile *MBopen( file, vrefnum, mode)
- char *file;
- short vrefnum,mode;
- {
- MBFile *mbfp;
- int err;
-
- if (strlen(file) > 63) /* BYU */
- return(0L); /* BYU */
-
- #if 0 /* BYU 2.4.12 */
- if (mode & MB_WRITE)
- putln("MBOpen for write");
- else
- putln("MBOpen for read");
-
- if (mode & MB_DISABLE)
- putln("MacBinary Protocol Disabled");
- #endif /* BYU 2.4.12 */
-
- (Ptr) mbfp = NewPtr( sizeof(MBFile));
- setmem(mbfp, sizeof(MBFile), 0); /* BYU 2.4.15 */
- if (mbfp==0L)
- return(0L);
-
- if (strlen(file) > 64) /* BYU 2.4.16 */
- movmem( file, mbfp->name, 64); /* BYU 2.4.16 */
- else /* BYU 2.4.16 */
- strcpy(mbfp->name, file); /* BYU 2.4.16 */
-
- putln(mbfp->name);
- c2pstr(mbfp->name);
- mbfp->vrefnum=vrefnum;
- mbfp->mode = mode;
-
- if ((err=FSOpen( mbfp->name, vrefnum, &mbfp->fd))) {
- if ((err==-43) && (mode & MB_WRITE)) {
- Create( mbfp->name, vrefnum, MyFavouriteTextEditor, 'TEXT');
- if (FSOpen( mbfp->name, vrefnum, &mbfp->fd))
- return( 0L);
- }
- else
- return(0L);
- }
-
- mbfp->binary=0;
- mbfp->sector1=1;
- mbfp->fork=0;
- return( mbfp);
- }
-
- int bwrite
- (
- MBFile *out,
- char *buffer,
- int size
- )
- {
- long len=size;
- int error;
-
- error = 0;
-
- if (out->binary) {
- if (out->bytes>0) {
- if (out->bytes < len) len = out->bytes;
- error= FSWrite( out->fd, &len, buffer);
- out->bytes -= len;
- buffer +=len;
- size -= (int)len;
- }
- if (out->bytes<= 0) {
- if (!out->fork) {
- out->fork=1;
- out->bytes=BLOCKS(out->rlen)*128;
- SetEOF( out->fd, (long) out->dlen);
- FSClose( out->fd);
- if (out->bytes) {
- OpenRF( out->name, out->vrefnum,&out->fd);
- if (size) {
- len = (long) size;
- error= FSWrite( out->fd, &len, buffer);
- }
- }
- else
- out->fd=0;
- }
- else SetEOF( out->fd, (long) out->rlen);
- }
- }
- else {
- error = FSWrite( out->fd, &len, buffer);
- }
- return (error);
- }
-
- void ProcessMBHead
- (
- MBFile *out,
- MBHead *header
- )
- {
- int err;
-
- movmem( header, &out->header, sizeof(MBHead));
- out->binary=1;
- lmove( &header->dflen[0], &out->dlen);
- lmove( &header->rflen[0], &out->rlen);
- out->bytes= BLOCKS(out->dlen)*128;
- out->fork=0;
- out->sector1=0;
-
- FSClose(out->fd);
- if (FSDelete( out->name, out->vrefnum))
- putln("Error Deleting Old File ");
-
- movmem( &out->header.nlen, out->name,63);
-
- #if 0 /* BYU 2.4.17 */
- MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
- #endif /* BYU 2.4.17 */
-
- if (out->bytes) {
- if ((err=FSOpen( out->name, out->vrefnum, &out->fd))) {
- if (err=-43) {
- long cre,typ;
-
- /* this crashes Mac Pluses
- typ = *(long *)out->header.type;
- cre = *(long *)out->header.creator;
- */
- lmove(out->header.type, &typ);
- lmove(out->header.creator, &cre);
-
- Create( out->name, out->vrefnum, cre,typ);
- if (FSOpen( out->name, out->vrefnum, &out->fd))
- return;
- }
- else {
- return;
- }
- }
- }
- else {
- if ((err=OpenRF( out->name, out->vrefnum, &out->fd))) {
- if (err=-43) {
- long cre,typ;
-
- /* this crashes Mac Pluses
- typ = *(long *)out->header.type;
- cre = *(long *)out->header.creator;
- */
- lmove(out->header.type, &typ);
- lmove(out->header.creator, &cre);
-
- Create( out->name, out->vrefnum, cre,typ);
- if (OpenRF( out->name, out->vrefnum, &out->fd))
- return;
- }
- else {
- return;
- }
- }
- out->fork = 1;
- out->bytes=BLOCKS(out->rlen)*128;
- }
- }
-
- int MBwrite
- (
- MBFile *out,
- void *buffer,
- int size
- )
- {
- int rsize;
-
- if (size < 1)
- return(0);
-
- rsize=size;
-
- if (out->sector1 && (size >= sizeof(struct MBHead)) &&
- isMacBinary(buffer) && (!(out->mode & MB_DISABLE))) {
- putln("First sector of MacBinary file");
- ProcessMBHead( out, (MBHead *) buffer);
- (Ptr) buffer+=128;
- if ((size-=128) <1)
- return(rsize);
- }
-
- if (bwrite( out,buffer,size))
- return(-1);
- else
- return( rsize);
- }
-
- void MBclose
- (
- MBFile *out
- )
- {
- FileParam finfo;
- long fpos;
-
- putln("MBclose");
-
- if (!out->fd)
- return;
-
- if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
- if (out->fork)
- SetEOF( out->fd, (long) out->rlen);
- else
- SetEOF( out->fd, (long) out->dlen);
-
- FSClose( out->fd);
- GetFileInfo( 0, out->name, &finfo);
-
- movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
- lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
- lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
- finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
- finfo.ioFlRLgLen=out->rlen;
- finfo.ioFlLgLen =out->dlen;
-
- SetFileInfo( 0, out->name, &finfo);
- }
- else if (out->mode & MB_WRITE) {
- GetFPos( out->fd, &fpos);
- SetEOF( out->fd, fpos);
- FSClose( out->fd);
- }
- else
- FSClose( out->fd);
- }
-
- int MBread
- (
- MBFile *in,
- void *buffer,
- int size
- )
- {
- char *p;
- int rsize=size;
-
-
- if (in->fork<0) {
- return(-1);
- }
-
- p=buffer;
- if (in->sector1) {
- FileParam finfo;
-
- setmem( &in->header, sizeof(MBHead), 0);
- movmem( in->name, &in->header.nlen, 64);
- GetFileInfo( in->vrefnum, in->name, &finfo);
- movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
- in->header.protected = (in->header.zero2 & 0x40)?1:0;
- in->header.zero2 = 0;
- lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
- lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
- lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
- lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
- in->dlen=finfo.ioFlLgLen;
- in->rlen=finfo.ioFlRLgLen;
- if (! (in->mode & MB_DISABLE) ) {
- if (size<128) return(-1);
-
- movmem( &in->header, p, 128);
- p +=128;
- size -= 128;
- in->bytes= BLOCKS(in->dlen)*128;
- in->binary=1;
- }
- else {
- in->bytes = in->dlen;
- in->rlen=0;
- in->binary=0;
- }
- in->sector1=0;
- #if 0 /* BYU 2.4.17 */
- MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
- #endif /* BYU 2.4.17 */
- }
-
- if ( size >0) {
- long length = (long)size;
- int err;
-
- err = FSRead( in->fd, &length, p);
-
- size -=(int)length;
- in->bytes -=length;
- p += length;
-
- if (err == -39 || (in->bytes<=0) ) {
- FSClose( in->fd );
- if (in->bytes<0L) in->bytes=0L;
- size -= (int)in->bytes;
- p += in->bytes; /* Make adjustments for necessary 128 byte term */
- if (!in->fork ) {
- in->fork=1;
- in->bytes= BLOCKS(in->rlen)*128;
- if (in->bytes) {
- OpenRF( in->name, in->vrefnum, &in->fd);
- #ifdef READ
- length=(long)size;
- if (length >0L) {
- err = FSRead( in->fd, &length, p);
- size -= (int)length;
- in->bytes -=length;
- }
- #endif READ
- }
- else {
- in->fd=0;
- in->fork=-1; /* Time to close up shop */
- }
- }
- else {
- in->fd=0;
- in->fork=-1; /* Time to close up shop */
- }
- }
- }
- return( rsize-size);
- }
-
- init_mb_files() { /* BYU */
- ftp_mbfp->fd = 0; /* BYU */
- mbfp->fd = 0; /* BYU */
- } /* BYU */
- /* BYU */
- close_mb_files() { /* BYU */
- if (ftp_mbfp->fd != 0) MBclose( ftp_mbfp ); /* BYU - close input file */
- if (mbfp->fd != 0) MBclose( mbfp ); /* BYU - close input file */
- } /* BYU */
-